package com.example.basicknowledge.键盘事件的响应;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.basicknowledge.R;

public class KeyboardAction extends Activity {
    private int imageAlpha;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keyboard_action);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                imageAlpha += 20;
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_DPAD_LEFT:
                imageAlpha -= 20;
                break;

        }
        if (imageAlpha > 100) imageAlpha = 100;
        if (imageAlpha < 0) imageAlpha = 0;
        ImageView imageView = findViewById(R.id.imageView);
        imageView.setAlpha((float) (imageAlpha/100.0));

        TextView textView = findViewById(R.id.textView2);
        textView.setText("Alpha - " + imageAlpha + "%");

        return super.onKeyDown(keyCode, event);
    }
}
